﻿using MengLin.OOP.DesignPattern.CompsitePattern.BasicCode.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MengLin.OOP.DesignPattern.CompsitePattern.BasicCode.Implements
{
    /// <summary>
    /// 树枝节点
    /// </summary>
    public class Composite : AbstractComponent
    {
        private List<AbstractComponent> children = new List<AbstractComponent>();

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="name"></param>
        public Composite(string name) : base(name)
        {

        }

        /// <summary>
        /// 添加叶子节点
        /// </summary>
        /// <param name="component"></param>
        public override void Add(AbstractComponent component)
        {
            children.Add(component);
        }

        /// <summary>
        /// 删除叶子节点
        /// </summary>
        /// <param name="component"></param>
        public override void Remove(AbstractComponent component)
        {
            children.Remove(component);
        }

        /// <summary>
        /// 显示部分与整体的层次结构
        /// </summary>
        /// <param name="depth">深度</param>
        public override void Display(int depth)
        {
            Console.WriteLine(new String('-', depth) + _name);
            foreach (AbstractComponent component in children)
            {
                component.Display(depth + 2);
            }
        }
    }
}
